package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"video-guard/models/mysql"

	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PrefaceUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPrefaceUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PrefaceUserLogic {
	return &PrefaceUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PrefaceUserLogic) PrefaceUser(in *videorpc.PrefaceUserRequest) (*videorpc.PrefaceUserResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.UserInfo
	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("未登录")
	}
	user = mysql.UserInfo{
		Model:      gorm.Model{ID: uint(in.UserId)},
		NickName:   in.NickName,
		AvatarLink: in.AvatarLink,
	}
	err = user.UpdatePassword(mysql.DB)
	if err != nil {
		return nil, errors.New("完善失败")
	}
	return &videorpc.PrefaceUserResponse{
		UserId: int64(user.ID),
	}, nil
}
